//API 操作
const API = require('../../datas/common.js');
let app = getApp();

//创建精选页面对象
Page({
    data: {
        noMoreData: false,
        seeMore: false,
        loadMore: false,
        hasGetLocation: false,
        totalContents: [],
        contents: [],
        showText: '',
        page: 1,
        pageSize: 10
    },
    onLoad: function () {
        let that = this;
        app.addNearbyListener(function (changedData) {
            let contents = that.data.contents;
            let index = contents.findIndex(function (element) {
                return element.openId = changedData.openId;
            })
            contents.splice(index, 1);
            that.setData({
                contents: changedData.concat(contents)
            });
        });
        API.showLoading('查找中...')
        that.getContentList(true);
    },
    onShow: function () {
    },
    onPullDownRefresh: function () {
        this.data.page = 1;
        this.getContentList(false);
    },
    showDetail: function (event) {
        wx.navigateTo({
            url: '/pages/nearby/detail/detail?type=NEARBY&id=' + event.currentTarget.dataset.id
        })
    },
    add: function (event) {
        API.getUserInfo().then(res => {
            wx.navigateTo({
                url: '/pages/nearby/add/add'
            })
        }).catch((res) => {
            API.showToast(res.message);
        })
    },
    getContentList: function (isOnload) {
        let that = this;
        API.getLastLocationLimit().then((res) => {
            API.get('/content/nearby', res).then(res => {
                if (isOnload) {
                    API.hideLoading();
                } else {
                    wx.stopPullDownRefresh();
                }
                that.data.totalContents = res.data;
                let contents = that.getContentByPage();
                let noMoreData = contents.length < that.data.pageSize;
                that.setData({
                    noMoreData: noMoreData,
                    seeMore: !noMoreData,
                    showText: '我也是有底线的',
                    hasGetLocation: true,
                    contents: contents
                })
            }).catch((res) => {
                that.getContentListError(isOnload, res.message);
            });
        }).catch((res) => {
            that.getContentListError(isOnload, res.message);
        });
    },
    getContentListError: function (isOnload, showText) {
        if (isOnload) {
            API.hideLoading();
        } else {
            wx.stopPullDownRefresh();
        }
        this.setData({
            noMoreData: true,
            seeMore: false,
            loadMore: false,
            showText: showText,
            contents: []
        });
    },
    up: function (event) {
        let that = this;
        API.getUserInfo().then(res => {
            let id = event.currentTarget.dataset.id;
            API.put('/content/up/' + id).then(res => {
                let index = event.currentTarget.dataset.index;
                let upTimes = 'contents[' + index + '].upTimes';
                let isUp = 'contents[' + index + '].isUp';
                that.setData({
                    [upTimes]: that.data.contents[index].upTimes + 1,
                    [isUp]: true,
                })
            }).catch((res) => {
                API.showToast(res.message);
            });
        }).catch((res) => {
            API.showToast(res.message);
        })
    },
    previewImage: function (event) {
        let index = event.currentTarget.dataset.index;
        let current = event.currentTarget.dataset.current;
        let images = this.data.contents[index].images;
        wx.previewImage({
            current: images[current], // 当前显示图片的http链接
            urls: images
        });
    },
    getContentByPage: function () {
        let page = this.data.page;
        let pageSize = this.data.pageSize;
        let totalContents = this.data.totalContents;
        let contents = totalContents.slice((page - 1) * pageSize, page * pageSize);
        this.data.page = page + 1;
        return contents;
    },
    onReachBottom: function () {
        let that = this;
        if (!that.data.noMoreData) {
            that.setData({
                seeMore: false,
                loadMore: true
            });
            let contents = that.getContentByPage();
            let noMoreData = contents.length < that.data.pageSize;
            setTimeout(function () {
                that.setData({
                    noMoreData: noMoreData,
                    seeMore: !noMoreData,
                    loadMore: false,
                    showText: '我也是有底线的',
                    contents: that.data.contents.concat(contents),
                })
            }, 1000)
        }
    }
});